package com.xbongbong.saas.model.impl;/*/*/

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.dao.WorkOrderTemplateCategoryDao;
import com.xbongbong.saas.domain.entity.WorkOrderTemplateCategoryEntity;
import com.xbongbong.saas.model.WorkOrderTemplateCategoryModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/3/5 14:36
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderTemplateCategoryModel")
public class WorkOrderTemplateCategoryModelImpl implements WorkOrderTemplateCategoryModel {
    @Resource
    private WorkOrderTemplateCategoryDao workOrderTemplateCategoryDao;

    @Override
    public Integer insert(WorkOrderTemplateCategoryEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return workOrderTemplateCategoryDao.insert(entity);
    }

    @Override
    public Integer update(WorkOrderTemplateCategoryEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        return workOrderTemplateCategoryDao.update(entity);
    }

    @Override
    public List<WorkOrderTemplateCategoryEntity> findEntitys(Map<String, Object> param) {
        return workOrderTemplateCategoryDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return workOrderTemplateCategoryDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer save(WorkOrderTemplateCategoryEntity entity) throws XbbException {
        if (entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public WorkOrderTemplateCategoryEntity getByKey(Long key, String corpid) {
        return workOrderTemplateCategoryDao.getByKey(key, corpid);
    }

    @Override
    public Integer sortCategory(Map<String, Object> param, Map<Long, Long> sortMap) {
        return workOrderTemplateCategoryDao.sort(param, sortMap);
    }

    @Override
    public Integer deleteBykey(Long key, String corpid) {
        return workOrderTemplateCategoryDao.deleteByKey(key, corpid);
    }


    @Override
    public List<WorkOrderTemplateCategoryEntity> getTemplateCategoryList(String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        //查询
        return findEntitys(param);
    }

    @Override
    public Map<String, String> getIdNameMap(String corpid) {
        List<WorkOrderTemplateCategoryEntity> list = getTemplateCategoryList(corpid);
        //模板id-模板名称
        Map<String, String> idNameMap = new HashMap<>(list.size());
        for (WorkOrderTemplateCategoryEntity entity : list) {
            idNameMap.put(entity.getId().toString(), entity.getName());
        }
        return idNameMap;
    }

    @Override
    public Integer updateBatch(List<WorkOrderTemplateCategoryEntity> entities, String corpid) {
       return workOrderTemplateCategoryDao.updateBatch(entities, corpid);
//        return null;
    }

    /**
     * 批量插入实体
     * @param list
     * @return 返回数据的列表
     */
    @Override
    public List<WorkOrderTemplateCategoryEntity> insertBatch(List<WorkOrderTemplateCategoryEntity> list) {
        workOrderTemplateCategoryDao.insertBatch(list);
        return list;
    }
}
